package com.example.controller;

import com.example.common.Result;
import com.example.entity.AirQualitySensor;
import com.example.entity.Params;
import com.example.entity.TemperatureHumiditySensor;
import com.example.service.AirQualitySensorService;
import com.example.service.TemperatureHumiditySensorService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@ResponseBody
@RestController
@CrossOrigin
@RequestMapping("/THS")
public class TemperatureHumiditySensorController {

    @Resource
    private TemperatureHumiditySensorService temperatureHumiditySensorService;


    @GetMapping("/getSensorData")
    public Result getSensorData(Params params) {
        PageInfo<TemperatureHumiditySensor> search = temperatureHumiditySensorService.getSensorData(params);
        return Result.success(search);
    }

    @GetMapping("/echarts/pie")
    public Result pie() {
        TemperatureHumiditySensor data = temperatureHumiditySensorService.initPie();
        return Result.success(data);
    }

    @GetMapping("/echarts/initLineForAdmin")
    public Result initLineForAdmin(Params params) {
        if (params.getPast() == null || params.getPast() <= 0) {
            params.setPast(3); ; // 给一个合理默认值
        }
        Map<String, Object> data = temperatureHumiditySensorService.initLineForAdmin(params);
        return Result.success(data);
    }

    @PostMapping("/addTHS")
    public Result addTHS(@RequestBody TemperatureHumiditySensor temperatureHumiditySensor){
       temperatureHumiditySensorService.addTHS(temperatureHumiditySensor);
        return Result.success();
    }

    @DeleteMapping("/deleteTHS/{id}")
    public Result deleteTHS(@PathVariable Long id){
        temperatureHumiditySensorService.deleteTHS(id);
        return Result.success();
    }
}
